//
//  XS_PlanChangeVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/28.
//

import UIKit

class XS_PlanChangeVC: XS_BaseVC {

    @IBOutlet weak var collectionView: UICollectionView!
    
    @IBOutlet weak var titleLabel: UILabel!
    var dataArray : [XS_PlanItemModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        shouldNavBarHidden = false
        title = "更改计划"
        let rightItem = UIBarButtonItem.init(title: "完成", style: .plain, target: self, action: #selector(doneBtnAction))
        rightItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.qmui_color(withHexString: "333333")!], for: .normal)
        navigationItem.rightBarButtonItem = rightItem
        collectionView.register(UINib.init(nibName: "XS_PlanChangeItemCell", bundle: Bundle.main), forCellWithReuseIdentifier: "XS_PlanChangeItemCell")
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight, right: 0)
        requestData()
    }


    @objc func doneBtnAction(){
        
        //        {
        //            "planDay":"计划学习天数",
        //            "id":"学习计划编号"
        //        }
        var tempArray : [[String:Any]] = []
        for model in dataArray {
            tempArray.append(["planDay":model.planDay,"id":model.id])
        }
        XS_NetWork.requestJson("/plan/updateLearnPlanIOS",parameters: ["learnPlanUpdateVOS":tempArray])
            .subscribe(onNext: { resp in //直接返回原始数据 resp.data
                if resp.success {
                    self.navigationController?.popViewController(animated: true)
                }
                xsWindow()?.makeToast(resp.msg)
            }).disposed(by: disposeBag)
        
    }
    
    func requestData(){
        XS_NetWork.requestJson("/plan/queryUserLearnPlan")
            .mapModels(type: XS_PlanItemModel.self)
            .subscribe(onNext: { array, resp in
                self.dataArray = array
                self.collectionView.reloadData()
                self.titleLabel.text = "我的学习计划 | "+String(array.count)+"门课"
            }).disposed(by: disposeBag)
    }

}

extension XS_PlanChangeVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XS_PlanChangeItemCell", for: indexPath) as! XS_PlanChangeItemCell
        if indexPath.item < dataArray.count {
            cell.model = dataArray[indexPath.item]
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: .screenWidth - 32, height: 144)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 8
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 16
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 16, left: 16, bottom: 16, right: 16)
    }
}
